package com.yummy.oneself.script;

import javax.script.ScriptException;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class ScriptExecutor {
	public static void main(String[] args) throws ScriptException, NoSuchMethodException {
		Context rhinoContext = Context.enter();
        try {
            Scriptable scope = rhinoContext.initStandardObjects();
            rhinoContext.evaluateString(scope, 
                "var $ = new Object(); $.add = function(a, b) { return a + b; }", 
                "script", 1, null);
            Object result = rhinoContext.evaluateString(scope, 
                "$.add(10, 20)", 
                "script", 1, null);
            System.out.println(Context.jsToJava(result, Integer.class)); // 输出 30
        } finally {
            Context.exit();
        }
    }
}
